package com.ling.pl.security.controller;

import com.ling.pl.core.commons.exception.BusinessException;
import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.AssertHelper;
import com.ling.pl.core.commons.utils.PageUtils;
import com.ling.pl.core.commons.utils.reflect.ReflectUtils;
import com.ling.pl.security.model.DefaultOrg;
import com.ling.pl.security.model.DefaultOrgVO;
import com.ling.pl.security.service.DefaultOrgService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
 * @author bo.wang
 */
public class BaseDefaultOrgController {
    @Resource
    DefaultOrgService defaultOrgService;

    Log log = LogFactory.getLog(BaseDefaultOrgController.class);

    @ApiOperation(value = "列出所有")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<DefaultOrgVO> findDefaultOrgByList(@RequestParam Map params) throws Exception {
        log.info("DefaultOrg#findByList");
        return defaultOrgService.findDefaultOrgVOByParams(params);
    }

    @ApiOperation(value = "分页列出")
    @RequestMapping(value = "/findByPage", method = RequestMethod.GET)
    public DaoPage findDefaultOrgByPage(@RequestParam Map params) throws Exception {
        log.info("DefaultOrg#findByPage");
        DaoPage daoPage = defaultOrgService.findDefaultOrgVOByParams(params, PageUtils.getPageIndex(params), PageUtils.getPageSize(params));
        return daoPage;
    }

    @ApiOperation(value = "新建一个")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @Transactional
    public DefaultOrgVO create(@ApiParam(value = "", required = true) @RequestBody DefaultOrgVO defaultOrgVO) {
        log.info("DefaultOrg#create");
        DefaultOrg entity = defaultOrgService.convertDefaultOrgVOTo(defaultOrgVO);
        defaultOrgService.save(entity);
        return defaultOrgService.convertDefaultOrgToVO(entity);
    }

    @ApiOperation(value = "获取单个")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @Transactional
    public DefaultOrgVO findById(@ApiParam(value = "ID") @PathVariable("id") String id) {
        log.info("DefaultOrg#findOne");
        AssertHelper.notEmpty_assert(id, "ID不能为空");
        DefaultOrg entity = (DefaultOrg) defaultOrgService.get(DefaultOrg.class, id);
        if (entity != null) {
            return defaultOrgService.convertDefaultOrgToVO(entity);
        } else {
            throw new BusinessException("没有找到指定ID的数据");
        }
    }

    @ApiOperation(value = "更新某个指定的实体")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @Transactional
    public DefaultOrgVO update(@ApiParam(value = "ID") @PathVariable("id") String id, @ApiParam(value = "更新的", required = true) @RequestBody DefaultOrgVO defaultOrgVO) {
        log.info("DefaultOrg#update");
        DefaultOrg entity = defaultOrgService.convertDefaultOrgVOTo(defaultOrgVO);
        defaultOrgService.update(entity);
        return defaultOrgService.convertDefaultOrgToVO(entity);
    }

    @ApiOperation(value = "更新指定主键数据（提供部分信息）")
    @RequestMapping(value = "/{ids_}", method = RequestMethod.PATCH)
    @Transactional
    public void updatePatch(@ApiParam(value = "ID") @PathVariable("ids_") String ids_, @ApiParam(value = "更新的", required = true) @RequestBody Map defaultOrgVO) {
        log.info("DefaultOrg#updatePatch");

    }

    @ApiOperation(value = "删除某个")
    @RequestMapping(value = "/{ids_}", method = RequestMethod.DELETE)
    @Transactional
    public void delete(@ApiParam(value = "删除的ID") @PathVariable("ids_") String ids_) {
        log.info("DefaultOrg#delete");
        String[] ids = ids_.split(",");
        for (String id : ids) {
            AssertHelper.notEmpty_assert(id, "ID不能为空");
            DefaultOrg entity = (DefaultOrg) defaultOrgService.get(DefaultOrg.class, id);
            defaultOrgService.remove(entity);
        }
    }

    @ApiOperation(value = "保存带状态的Map数据集合")
    @RequestMapping(value = "/saveMap", method = RequestMethod.POST)
    @Transactional
    public void saveMap(@ApiParam(value = "", required = true) @RequestBody Map<String, List<DefaultOrgVO>> dataListsMap) {
        defaultOrgService.saveDefaultOrgDataListsMap(dataListsMap);
    }

    @ApiOperation(value = "保存数据集合")
    @RequestMapping(value = "/saveAll", method = RequestMethod.POST)
    @Transactional
    public void saveAll(@ApiParam(value = "", required = true) @RequestBody List<DefaultOrgVO> dataLists) {
        if (dataLists != null && dataLists.size() > 0) {
            for (DefaultOrgVO vo : dataLists) {
                DefaultOrg entity = defaultOrgService.convertDefaultOrgVOTo(vo);
                defaultOrgService.save(entity);
            }
        }
    }


    public DaoPage loadDefaultOrgPage(Map parameter, Integer pageIndex,
                                      Integer pageSize) throws Exception {
        DaoPage daoPage = defaultOrgService.findDefaultOrgByParams(parameter, pageIndex, pageSize);
        return daoPage;
    }

    public Collection<DefaultOrgVO> loadDefaultOrgList(Map parameter) throws Exception {
        List result = defaultOrgService.findDefaultOrgByParams(parameter);
        return result;
    }

    public DefaultOrgVO loadAddDefaultOrg(Map parameter) throws Exception {
        DefaultOrgVO vo = new DefaultOrgVO();
        return vo;
    }

    public DefaultOrgVO loadModifyDefaultOrg(Map parameter) throws Exception {
        Object id = parameter.get("id");
        return loadModifyDefaultOrg(id);
    }

    public DefaultOrgVO loadModifyDefaultOrg(Object id) throws Exception {
        AssertHelper.notEmpty_assert(id, "编辑的主键不能为空");
        DefaultOrg entity = (DefaultOrg) defaultOrgService.get(DefaultOrg.class, id.toString());
        DefaultOrgVO vo = defaultOrgService.convertDefaultOrgToVO(entity);
        return vo;
    }

    public void saveDefaultOrg(Collection<DefaultOrgVO> objs) throws Exception {
        Map results = PageUtils.assembleDatasetMap(objs);
        defaultOrgService.saveDefaultOrgDataListsMap(results);
    }

    public void saveAddDefaultOrg(DefaultOrgVO vo) throws Exception {
        DefaultOrg entity = defaultOrgService.convertDefaultOrgVOTo(vo);
        if (entity.getId() == null) {
            defaultOrgService.save(entity);
        } else {
            defaultOrgService.update(entity);
        }
        vo.setId(entity.getId());
    }

    public void updateModifyDefaultOrg(DefaultOrgVO vo) throws Exception {
        DefaultOrg entity = (DefaultOrg) defaultOrgService.get(DefaultOrg.class, vo.getId());
        ReflectUtils.copyProperties(vo, entity);
        defaultOrgService.update(entity);
    }
}

